package com.ysd.lis.service.emr.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.emr.EmrDto;
import com.ysd.lis.entity.emr.EmrItemStandard;
import com.ysd.lis.mapper.emr.EmrItemStandardMapper;
import com.ysd.lis.request.emr.EmrParam;
import com.ysd.lis.service.emr.EmrItemStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.sys_Model 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-12-27
 */
@Service
public class EmrItemStandardServiceImpl extends ServiceImpl<EmrItemStandardMapper, EmrItemStandard> implements EmrItemStandardService {

    @Autowired
    EmrItemStandardMapper emrItemStandardMapper;
    @Override
    public Result addItemStandard(EmrItemStandard emrItemStandard) {
        int insert = emrItemStandardMapper.insert(emrItemStandard);
        if(insert>0){
            return Result.succ(1,"添加成功",emrItemStandard);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editItemStandard(EmrItemStandard supplier) {
        int updateById = emrItemStandardMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result findEmrItemStandardList(EmrParam param) {
        MPJLambdaWrapper<EmrItemStandard> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(EmrItemStandard.class)
                .eq(EmrItemStandard::getDelFlag,0);
        if(StringUtils.isNotBlank(param.getVersionCode())){
            queryWrapper.eq(EmrItemStandard::getVersionCode, param.getVersionCode());
        }
        if(StringUtils.isNotBlank(param.getRoleCode())){
            queryWrapper.eq(EmrItemStandard::getRoleCode, param.getRoleCode());
        }
        if(StringUtils.isNotBlank(param.getItemCode())){
            queryWrapper.eq(EmrItemStandard::getItemCode, param.getItemCode());
        }
        queryWrapper.orderByAsc(EmrItemStandard::getSeq);
        List<EmrItemStandard> emrItemStandardList = emrItemStandardMapper.selectList(queryWrapper);
        return Result.succ(1, "成功", emrItemStandardList);
    }

    @Override
    public Result batchDeleteItemStandard(EmrDto dto) {
        List<EmrItemStandard> emrItemStandardList = dto.getEmrItemStandardList();
        List<String> ids = emrItemStandardList.stream().map(EmrItemStandard::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
